/**
 * Copyright (c) 2006 RiseSoft Co.,Ltd  
 * $Header$
 */
package net.risesoft.risebi.collection.web;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import net.risesoft.risebi.collection.model.Attachment;
import net.risesoft.risebi.collection.service.AttachmentManager;
import net.risesoft.risebi.commons.util.ExceptionUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 *
 * @author <a href="mailto: chenjian@risesoft.net">陈健</a>
 * @version $Revision$
 * 
 */
public class BaseAttachmentAction extends BaseAction {

	private static final long serialVersionUID = -3288488057395442903L;
	private static Log log = LogFactory.getLog(BaseAttachmentAction.class);

	@Action(value="download",
			results={@Result(name="success", type="stream",params={"contentType","${contentType}","inputName","inputStreamName","bufferSize","4096","contentDisposition","${contentDisposition}"})}
	)
	public String download() throws Exception {
		ByteArrayInputStream bais = null;
		try{
			Attachment attachment = attachmentManager.getAttachment(node, true);
			
			byte[] b = attachment.getContent();
			bais = new ByteArrayInputStream(b);
			this.inputStreamName = bais;
			this.contentType = "application/octet-stream";
			String encodeFileName = new String((attachment.getName()).getBytes("gb2312"),"ISO8859-1");
			this.contentDisposition = "attachment; filename="+encodeFileName;
			return SUCCESS;
		}catch(Exception e){
			log.error(ExceptionUtil.transformException(e));
		}
		return SUCCESS;
	}
	
	private AttachmentManager attachmentManager;
	
	public void setAttachmentManager(AttachmentManager attachmentManager) {
		this.attachmentManager = attachmentManager;
	}

	private String contentType;
	private InputStream inputStreamName;
	private String contentDisposition;
	
	public String getContentType() {
		return contentType;
	}
	public void setContentType(String contentType) {
		this.contentType = contentType;
	}
	public InputStream getInputStreamName() {
		return inputStreamName;
	}
	public void setInputStreamName(InputStream inputStreamName) {
		this.inputStreamName = inputStreamName;
	}
	public String getContentDisposition() {
		return contentDisposition;
	}
	public void setContentDisposition(String contentDisposition) {
		this.contentDisposition = contentDisposition;
	}

}
